package com.shouhe.mysap.controller.plan;


import com.shouhe.mysap.common.MySapResult;
import com.shouhe.mysap.common.PageResult;
import com.shouhe.mysap.entity.PlanPerson;
import com.shouhe.mysap.service.PlanPersonService;
import com.shouhe.mysap.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 守鹤
 * @since 2020-06-05
 */
@RestController
@RequestMapping("/plan-person")
public class PlanPersonController {
    @Autowired
    private PlanPersonService planPersonService;

    /**
     * @描述 得到全部的计划人员
     * @参数
     * @返回值
     */
    @RequestMapping("/getPlanPersons")
    public PageResult getPlanPersons(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer rows) {
        PageResult pageResult = PageResultUtil.getPageResult(page, rows, planPersonService);
        return pageResult;
    }

    /**
     * @描述 通过计划组得到计划人员
     * @参数
     * @返回值
     */
    @RequestMapping("/getPlanPersonByPlanGroup")
    public PlanPerson getPlanPersonByPlanGroup(Integer PlanGroup) {
        PlanPerson PlanPerson = SelectUtil.selectOneEq("plan_group", PlanGroup, planPersonService);
        return PlanPerson;
    }

    /**
     * @描述 通过名字得到计划人员
     * @参数
     * @返回值
     */
    @RequestMapping("/getPlanPersonByPlanName")
    public PlanPerson getPlanPersonByPlanName(String PlanName) {
        PlanPerson PlanPerson = SelectUtil.selectOneEq("plan_name", PlanName, planPersonService);
        return PlanPerson;
    }


    /**
     * @描述 得到全部的计划组
     * @参数
     * @返回值
     */
    @RequestMapping("/getPlanGroups")
    public List<Integer> getPlanGroups() {
        List<PlanPerson> persons = planPersonService.list(null);
        List<Integer> list = new ArrayList<>();
        for (PlanPerson person : persons) {
            list.add(person.getPlanGroup());
        }
        return list;
    }


    /**
     * @描述 添加一个计划人员
     * @参数
     * @返回值
     */
    @RequestMapping("/addPlanPerson")
    public MySapResult addPlanPerson(PlanPerson PlanPerson) {
        //首先判断计划组是否已经存在
        PlanPerson person = getPlanPersonByPlanGroup(PlanPerson.getPlanGroup());
        if (person != null) {
            return MySapResult.fail("添加计划员失败,计划组已经存在");
        }
        return AddUtil.addObject("添加计划员",PlanPerson,planPersonService);
    }

    /**
     * @描述 通过 id数组 删除计划员
     * @参数
     * @返回值
     */
    @RequestMapping("/deletePlanPersons")
    public MySapResult deletePlanPersons(@RequestParam("ids[]") Integer[] ids) {
        return  DeleteUtil.deleteMany("删除计划员",ids,planPersonService);
    }

    /**
     * @描述 通过id删除 计划员
     * @参数
     * @返回值
     */
    @RequestMapping("/updatePlanPerson")
    public MySapResult updatePlanPerson(PlanPerson PlanPerson) {
        //首先判断计划组是否已经存在
        PlanPerson person = getPlanPersonByPlanGroup(PlanPerson.getPlanGroup());
        if (person != null) {
            return MySapResult.fail("更新计划员失败,计划组已经存在");
        }
        return UpdateUtil.updateObjectById("更新计划员",PlanPerson,planPersonService);
    }

}

